/*
 * Copyright (c) 2012 Mobs & Geeks
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mobsandgeeks.adapters;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

/**
 * CircularListAdapter
 *
 * @since 2021-07-02
 */
public class CircularListAdapter extends BaseItemProvider {
    static final String TAG = CircularListAdapter.class.getSimpleName();
    private InstantAdapter mListAdapter;
    private int mListAdapterCount;

    /**
     * 构造
     *
     * @param listAdapter
     */
    public CircularListAdapter(InstantAdapter listAdapter) {
        if (listAdapter == null) {
            LogUtils.log(LogUtils.DEBUG,TAG,"listAdapter cannot be null.");
        }
        this.mListAdapter = listAdapter;
        this.mListAdapterCount = listAdapter.getCount();
    }

    @Override
    public int getCount() {
        return mListAdapterCount;
    }

    @Override
    public Object getItem(int position) {
        return mListAdapter.getItem(position % mListAdapterCount);
    }

    @Override
    public long getItemId(int position) {
        return mListAdapter.getItemId(position % mListAdapterCount);
    }

    @Override
    public int getItemComponentType(int position) {
        return mListAdapter.getItemComponentType(position % mListAdapterCount);
    }

    @Override
    public int getComponentTypeCount() {
        return mListAdapter.getComponentTypeCount();
    }

    @Override
    public void notifyDataChanged() {
        mListAdapter.notifyDataChanged();
        mListAdapterCount = mListAdapter.getCount();
        super.notifyDataChanged();
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        return mListAdapter.getComponent(position % mListAdapterCount, component, componentContainer);
    }
}
